package com.jcnet.bizaia.fragment;

import java.util.List;
import java.util.Timer;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.jcnet.bizaia.App;
import com.jcnet.bizaia.R;
import com.jcnet.bizaia.bean.MusicStateBean;
import com.jcnet.bizaia.bean.VoiceDetailBean;
import com.jcnet.bizaia.service.JCMusicPlayerService;
import com.jcnet.bizaia.service.JCMusicPlayerService.LocalBinder;
import com.jcnet.bizaia.tools.MusicTools;
import com.squareup.picasso.Picasso;

public final class MusicMediaControllerFragment extends Fragment implements OnClickListener{
	private static final String KEY_CONTENT = "TestFragment:Content";

	public JCMusicPlayerService musicPlayerService;

	private boolean isbind  = false;


	private ImageButton previousButton;
	private ImageButton playButton;
	private ImageButton nextButton;
	
	private ImageView voiceImageView;

	private SeekBar seekBar;

	private TextView voiceNameTextView;


	private List<VoiceDetailBean> musicBeans;

	private int musicPoistion  = 0; 

	private Timer timer;

	public static final String UPDATA_SEEKBAR_RECEIVER = "upDataSeekBarSizeReceiver";
	public static final String CHANGE_NAME_IMG_AUTHOR = "CHANGE_NAME_IMG_AUTHOR" ;

	private BroadcastReceiver broadcastReceiver;

	private MusicStateBean musicStateBean;  //保存当前更新界面需要的数据
	
	
	public MusicMediaControllerFragment() {
		super();
	}

	public static MusicMediaControllerFragment newInstance(List<VoiceDetailBean> musicBeans,int musicPoistion) {
		MusicMediaControllerFragment fragment = new MusicMediaControllerFragment();
		fragment.musicBeans = musicBeans;
		fragment.musicPoistion = musicPoistion;
		return fragment;
	}



	private String mContent = "???";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
			mContent = savedInstanceState.getString(KEY_CONTENT);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = View.inflate(getActivity(), R.layout.music_controller_lay, null);

		previousButton = (ImageButton)view.findViewById(R.id.controlPreviousButton);

		playButton  = (ImageButton)view.findViewById(R.id.controlPlayButton);

		nextButton = (ImageButton)view.findViewById(R.id.controlNextButton);

		seekBar =(SeekBar)view.findViewById(R.id.controlSeekBar);
		
		voiceNameTextView = (TextView)view.findViewById(R.id.voiceName);
		
		voiceImageView = (ImageView)view.findViewById(R.id.voiceImg);

		previousButton.setOnClickListener(this);

		playButton.setOnClickListener(this);

		nextButton.setOnClickListener(this);
		
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
				
				musicPlayerService.seekTo(seekBar.getProgress());
				seekBar.setProgress(seekBar.getProgress());
				musicPlayerService.play();;
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				if(musicPlayerService.getMediaPlayer().isPlaying()){
					musicPlayerService.getMediaPlayer().pause();
				}
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
			}
		});
		

		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(UPDATA_SEEKBAR_RECEIVER);
		intentFilter.addAction(CHANGE_NAME_IMG_AUTHOR);

		broadcastReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub

				if(intent.getAction().equals(UPDATA_SEEKBAR_RECEIVER)){
					musicStateBean = (MusicStateBean) intent.getSerializableExtra("musicSate");
					if(musicStateBean!=null){
						seekBar.setMax((int)musicStateBean.getDuration());
						seekBar.setProgress((int)musicStateBean.getCurrentPosition());
					}
					
					//  设置图标状态
					if(musicPlayerService.getMediaPlayer().isPlaying()){ 
						playButton.setImageResource(R.drawable.control_play_bgg);
					}else{  
						playButton.setImageResource(R.drawable.control_pause_bgg);
					}
					
					
					
				/*	System.out.println(musicPlayerService.voiceDetailBean.getTitle()+"*********");
					System.out.println(musicPlayerService.voiceDetailBean.getVideourl()+"*********");*/
					
				}
				if(intent.getAction().equals(CHANGE_NAME_IMG_AUTHOR)){
					System.out.println("?????????????????))))))))))))))))");
					voiceNameTextView.setText(musicPlayerService.voiceDetailBean.getTitle());
					Picasso.with(getActivity()).load(App.HTTP_PATH+musicPlayerService.voiceDetailBean.getAcsimages()).centerCrop().resize(50, 50).into(voiceImageView);
					System.out.println("????"+App.HTTP_PATH+musicPlayerService.voiceDetailBean.getAcsimages());
				}
				
				
			}
		};

		getActivity().registerReceiver(broadcastReceiver, intentFilter);

		return view;
	}

@Override
public void onDestroy() {
	// TODO Auto-generated method stub
	super.onDestroy();
	
	getActivity().unregisterReceiver(broadcastReceiver);
}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(KEY_CONTENT, mContent);
	}


	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		if(!isbind){
			Intent intent = new Intent(getActivity(), JCMusicPlayerService.class);
			getActivity().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
			isbind = true;
			System.out.println("bind service_____________________");
		}	
		
		

	}

	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			isbind = false;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			LocalBinder binder = (LocalBinder) service;
			musicPlayerService = binder.getService();
			isbind = true;
			System.out.println("****************************************");
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.controlPlayButton:
			play();
			break;
		case R.id.controlPreviousButton:  //播放上一首
			MusicTools.musicPrevious(getActivity());
			break;

		case R.id.controlNextButton:     //播放下一首
			MusicTools.musicNext(getActivity());
			break;

		default:
			break;
		}

	}


	public void play(){

		if(musicPlayerService.getMediaPlayer().isPlaying()){   //暂停播放
			MusicTools.musicPause(getActivity());
		}else{   //开始播放
			MusicTools.musicPlay(getActivity());
		}

	}
}
